<?php
// +----------------------------------------------------------------------
// | zhanshop-cloud / Cloud.php    [ 2024/8/18 21:00 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\task;

use app\api\admin\v1\provider\CloudappProvider;
use extend\docker\Container;
use Swoole\Coroutine;
use zhanshop\App;
use zhanshop\client\Httpclient;
use zhanshop\util\WebHook;

class CloudTask
{
    /**
     * 统计docker使用使用情况
     * @return void
     * @throws \Exception
     */
    public function appStats()
    {
        $list = App::make(CloudappProvider::class)->getList();
        $chunkList = array_chunk($list['list'], 16);
        foreach ($chunkList as $list){
            foreach ($list as $v){
                if(isset($v['internal_network']) && $v['internal_network']){
                    Coroutine::create(function () use ($v){
                        $arr = (new Container())->stats($v['id']);
                        error_log(date('H:i:s')."###".json_encode($arr)."\n", 3, App::runtimePath().'/cloud/stats/'.$v['id'].'-'.date("Ymd").'.log');
                    });
                }
            }
        }
    }

    /**
     * 统计nginx日志文件IP排行榜和错误码
     * @param string $accessPath
     * @return void
     */
    public function nginxStats(string $accessPath)
    {
        if(file_exists($accessPath)){
            $stats= new \extend\stats\NginxStats();
            $slice = $stats->slice($accessPath);

            $statsData = [
                'area_stats' => [],
                'uri_stats' => [],
                'ip_stats' => [],
            ];

            $statsData['area_stats'] = $stats->ipSliceAreaStats($slice['ip']); // 拿到了地区分布
            $statsData['uri_top1000'] = $stats->uriSliceTop1000Stats($slice['uri']); // 拿到了uri排行榜
            $statsData['ip_top1000'] = $stats->ipSliceTop1000Stats($slice['ip']); // 拿到了ip排行榜

            $statJsonFile = App::rootPath().'/docker/nginx/stats/stats-'.date('Ymd', strtotime('-1 day')).'.log';
            file_put_contents($statJsonFile, json_encode($statsData));
        }
    }
    protected $clientIp = "";
    public function devopsStatus()
    {
        if($this->clientIp == false){
            $ipBody = (new Httpclient())->setTimeout(3000)->request('http://4.ipw.cn/');
            if($ipBody['code'] == 200){
                $this->clientIp = $ipBody['body'];
            }
        }
        $classs = scandir(App::rootPath().'/extend/devops');
        $devopsValve = floatval(App::env()->get("DEVOPS_VALVE", "0.8"));
        foreach ($classs as $class){
            $info = pathinfo($class);
            if($info['extension'] == 'php'){
                $filename = $info['filename'];
                App::make('\\extend\\devops\\'.$filename)->inspect($devopsValve, function ($usageRate)use ($filename){
                    App::make(WebHook::class)->weChat('', 'text', $this->clientIp.'的'.$filename.'空间使用率达到'.($usageRate * 100).'%');
                });
            }
        }
    }

    public function crontab()
    {

    }
}